package dongnv.poweramp.data;

import java.io.File;
import java.util.ArrayList;

import dongnv.poweramp.R;
import dongnv.poweramp.activity.HomeActivity;
import dongnv.poweramp.activity.ListFolderActivity;
import dongnv.poweramp.activity.ListSongPlayActivity;
import dongnv.poweramp.screen.HomeScreen;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListFolderAdapter extends ArrayAdapter<FolderItem> {

	public ListFolderAdapter(Context context, int textViewResourceId, ArrayList<FolderItem> arrayFolderItem) {
		super(context, textViewResourceId, arrayFolderItem);
		this.context = context;
		this.arrayFolderItem = arrayFolderItem;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int currentFolderIndex = position;
		View view = View.inflate(context, R.layout.folder_icon, null);
		folderName = (TextView) view.findViewById(R.id.folderName);
		folderPath = (TextView) view.findViewById(R.id.folderPath);
		countSong = (TextView) view.findViewById(R.id.countSong);
		
		folderName.setText(arrayFolderItem.get(position).getName());
		folderPath.setText(arrayFolderItem.get(position).getPath());
		countSong.setText("Số bài hát: " + arrayFolderItem.get(position).getCountSong());
		
		
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HomeActivity.systemManager.setCurrentFolderItem(arrayFolderItem.get(currentFolderIndex));
				HomeActivity.systemManager.getArraySongItem().clear();
				File currentFile = new File(HomeActivity.systemManager.getCurrentFolderItem().getPath());
				HomeActivity.systemManager.scanSong(currentFile);
				
				Intent intent = new Intent(context, ListSongPlayActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			} 
		});
		return view;
	}

	private Context context;
	private ArrayList<FolderItem> arrayFolderItem;
	private TextView folderName, folderPath, countSong;
}
